<template>
  <div ref="chart" style="height: 100%;" />
</template>
<script>
import { Chart, registerShape } from '@antv/g2';
export default {
  name: 'RoseChart',
  props: {
    data: { type: Array, required: true },
    color: { type: Array, required: true }
  },
  data() {
    return {
      chart: null,
      geo: null
    };
  },
  watch: {
    data: {
      handler() {
        this.updataData();
      }
    }
  },
  mounted() {
    this.initChart();
    this.drawChart();
  },
  methods: {
    initChart() {
      const ref = this.$refs.chart;
      this.chart = new Chart({
        container: ref,
        width: ref.clientWidth,
        height: ref.clientHeight
      });
    },
    drawChart() {
      const ratio = window.ratio || 1;
      registerShape('interval', 'slice-shape', {
        draw(cfg, container) {
          const points = cfg.points;
          let path = [];
          path.push(['M', points[0].x, points[0].y]);
          path.push(['L', points[1].x, points[1].y]);
          path.push(['L', points[2].x, points[2].y]);
          path.push(['L', points[3].x, points[3].y]);
          path.push('Z');
          path = this.parsePath(path);
          return container.addShape('path', {
            attrs: {
              fill: cfg.color,
              fillOpacity: 0.5,
              path
            }
          });
        }
      });
      // const dataArr = this.data.map((item) => {
      //   return item.y;
      // });
      // 求数据总和
      // const total = dataArr.reduce((total, value) => {
      //   return total + value;
      // });
      this.chart.data(this.data);
      this.chart.legend(false);
      // x轴
      this.chart.axis('x', {
        label: null,
        grid: null,
        line: {
          style: {
            stroke: '#FFFFFF',
            strokeOpacity: 0
          }
        }
      });
      // y轴
      this.chart.axis('y', {
        line: null,
        label: null,
        grid: {
          line: {
            style: {
              stroke: '#FFFFFF',
              strokeOpacity: 0.1
            }
          },
          alignTick: true
        }
      });
      this.chart.scale('y', {
        tickCount: 6,
        nice: true
      });
      this.chart.coordinate('polar');
      this.geo = this.chart
        .interval()
        .position('x*y')
        .color('x', this.color)
        .shape('slice-shape')
        .style({
          fillOpacity: 0.5
        })
        .state({
          active: {
            style: {
              lineWidth: 0,
              shadowColor: '#fff',
              shadowBlur: 5 * ratio
            }
          },
          inactive: {
            style: {
              fillOpacity: 0.3
            }
          },
          selected: {
            style: {
              lineWidth: 0,
              opacity: 0
            }
          }
        });
      this.chart.tooltip({
        showMarkers: null,
        showTitle: false,
        domStyles: {
          'g2-tooltip-title': {
            display: 'none',
            fontSize: '.875rem',
            color: '#8D8D8D'
          },
          'g2-tooltip-list-item': {
            fontSize: 0,
            overflow: 'hidden',
            margin: '.625rem 0 .3125rem 0'
          },
          'g2-tooltip-name': {
            fontSize: '.75rem',
            color: '#8D8D8D'
          },
          'g2-tooltip-value': {
            fontSize: '.75rem',
            color: 'rgba(0, 0, 0, 0.6)',
            fontWeight: '780'
          }
        }
      });
      if (!this.data.some(item => item.y !== 0)) {
        this.chart.scale('y', {
          ticks: [0, 1, 2, 3, 4, 5, 6]
        });
      }
      this.chart.render();
    },
    updataData() {
      this.chart.changeData(this.data);
    }
  }
};
</script>
